﻿#region  文件信息

// ---------------------------------------------------------------------------------------
// <copyright file="WxCommon.cs" company="">
// 作者：李帅伦
// </copyright>
// <summary>
// 类型简介：
// 类型详细：WxCommon.cs
// 创建时间：2014-12-25 12:59
// 修改时间：2014-12-23 17:06
// </summary>
// ---------------------------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;
using System.Xml;
using AmazeUI.bll.Open.Weixin;
using Newtonsoft.Json.Linq;
using WebCommon;

namespace AmazeUI.bll.Open.WeixinPay
{
    

    /// <summary>
    /// The wx common.
    /// </summary>
    public class WxCommon
    {
        #region Public Methods and Operators
        /// <summary>
        /// The get string sign temp.
        /// </summary>
        /// <param name="str">
        /// The str.
        /// </param>
        /// <param name="paternerKey">
        /// The paterner key.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public static string GetStringSignTemp(string str, string paternerKey)
        {
            var md5 = str + "&key=" + paternerKey;
            return Encryption.Md532(md5).ToUpper();
        }
        public static string GetStringSignTempSha1(string str, string paternerKey)
        {
            var sh1 = str + "&key=" + paternerKey;
            return Encryption.Sha1(sh1).ToLower();
        }
        public static SortedDictionary<string, string> FromXml(string xml)
        {
            var sortedDictionary = new SortedDictionary<string, string>();
            if (string.IsNullOrEmpty(xml))
            {
                throw new Exception("将空的xml串转换为WxPayData不合法!");
            }

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);
            XmlNode xmlNode = xmlDoc.FirstChild;//获取到根节点<xml>
            XmlNodeList nodes = xmlNode.ChildNodes;
            foreach (XmlNode xn in nodes)
            {
                XmlElement xe = (XmlElement)xn;
                sortedDictionary.Add(xe.Name, xe.InnerText); //获取xml的键值对到WxPayData内部的数据中
            } 
            return sortedDictionary;
        }

        /// <summary>
        /// 转成URL
        /// </summary>
        /// <param name="sd"></param>
        /// <returns></returns>
        public static string ToUrl(SortedDictionary<string, string> sd)
        {
            string buff = "";
            foreach (var pair in sd)
            {

                if (pair.Key != "sign" && pair.Value != "")
                {
                    buff += pair.Key + "=" + pair.Value + "&";
                }
            }
            buff = buff.Trim('&');
            return buff;
        }



        #endregion

        /// <summary>
        ///      接收从微信后台POST过来的数据
        /// </summary> 
        /// <param name="stream"></param>
        public static string GetInputStream(System.IO.Stream stream)
        {
            System.IO.Stream s = stream;
            int count = 0;
            byte[] buffer = new byte[1024];
            StringBuilder builder = new StringBuilder();
            while ((count = s.Read(buffer, 0, 1024)) > 0)
            {
                builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
            }
            s.Flush();
            s.Close();
            s.Dispose();
            try
            {
               return  builder.ToString() ;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}